﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.Collections.Generic;
using System.Linq;
using WZN_shop.Models.Database;
using WZN_shop.service;

namespace WZN_shop.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class ProductControllers : ControllerBase
    {
        private readonly IJWTService _jwtService;
        private readonly WZN_ShopContext _db;
        public ProductControllers(IJWTService jwtService, WZN_ShopContext db)
        {
            _jwtService = jwtService;
            _db = db;
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="goodname"></param>
        /// <param name="goodimg"></param>
        /// <param name="goodprice"></param>
        /// <param name="goodinventory"></param>
        /// <param name="goodbrief"></param>
        /// <param name="goodclass"></param>
        /// <param name="goodstatic"></param>
        /// <returns></returns>
        [HttpGet]
        public string AddProducts(string goodname,string goodimg,decimal goodprice,int goodinventory,string goodbrief,string goodclass,string goodstatic)
        {
            Good good=new Models.Database.Good();
            good.GoodName = goodname;
            good.GoodImg = goodimg;
            good.GoodPrice = goodprice;
            good.GoodInventory = goodinventory;
            good.GoodBrief = goodbrief;
            good.GoodClass = goodclass;
            good.GoodStatic = goodstatic;
            if (goodstatic != "上架" || goodstatic != "下架")
            {
                return "请填写商品状态";
            }
            _db.Goods.Add(good);
            if (_db.SaveChanges() > 0)
            {
                return "添加成功";
            }
            else
            {
                return "添加失败";
            }

        }

        /// <summary>
        ///删除商品
        /// </summary>
        /// <param name="goodid"></param>
        /// <returns></returns>
        [HttpGet]
        public string DetalProducts(int goodid)
        {
            var good = _db.Goods.FirstOrDefault(x => x.GoodId == goodid);
            if (good != null)
            {
                _db.Goods.Remove(good);
                return "删除成功";
            }
            else
            {
                return "不存在该商品";
            }
        }


        /// <summary>
        /// 修改商品
        /// </summary>
        /// <param name="goodid"></param>
        /// <param name="goodname"></param>
        /// <param name="goodimg"></param>
        /// <param name="goodprice"></param>
        /// <param name="goodinventory"></param>
        /// <param name="goodbrief"></param>
        /// <param name="goodclass"></param>
        /// <param name="goodstatic"></param>
        /// <returns></returns>
        [HttpPost]
        public string ChangeProduct(int goodid, string goodname, string goodimg, decimal goodprice, int goodinventory, string goodbrief, string goodclass, string goodstatic)
        {
            var good = _db.Goods.FirstOrDefault(x => x.GoodId == goodid);
            if (good == null)
            {
                return "该商品不存在";
            }
            if (goodname != null) good.GoodName = goodname;
            if (goodimg != null) good.GoodImg = goodimg;
            if (goodprice != 0) good.GoodPrice = goodprice;
            if(goodinventory!=0)good.GoodInventory = goodinventory;
            if(goodbrief!=null)good.GoodBrief = goodbrief;
            if(goodclass!=null) good.GoodClass = goodclass;
            if(goodstatic!=null) good.GoodStatic = goodstatic;
            if (_db.SaveChanges() > 0)
            {
                return "修改成功";
            }
            else
            {
                return "修改失败";
            }
        }


        /// <summary>
        /// 查询商品
        /// </summary>
        /// <param name="goodid"></param>
        /// <param name="goodclass"></param>
        /// <returns></returns>
        [HttpGet]
        public List<Good> SelectProducts(int goodid,string goodclass)
        {
            if (goodid == 0&&goodclass!=null)
            {
                return _db.Goods.Where(x=>x.GoodClass== goodclass).ToList(); 
            }else if (goodclass == null&&goodid!=0)
            {
                return _db.Goods.Where(x=>x.GoodId== goodid).ToList();
            }
            else
            {
                return _db.Goods.ToList();
            }
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="classifid"></param>
        /// <param name="GoodClass"></param>
        /// <returns></returns>
        [HttpGet]
        public string AddClassifi(string classifid,string GoodClass)
        {
            var Classifi = new Models.Database.Classification();
            Classifi.Classifi = classifid;
            Classifi.GoodClass= GoodClass;
            if(_db.Classifications.FirstOrDefault(x=>x.GoodClass == GoodClass) != null)
            {
                return "该分类已存在";
            }
            else
            {
                _db.Classifications.Add(Classifi);
                _db.SaveChanges();
                return "添加成功";
            }
        }


        
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="GoodClass"></param>
        /// <returns></returns>
        [HttpGet]
        public string DetalClassifi(string GoodClass)
        {
            var classifi = _db.Classifications.FirstOrDefault(x => x.GoodClass == GoodClass);
            if (classifi == null)
            {
                return "分类不存在";
            }
            else
            {
                _db.Classifications.Remove(classifi);
                _db.SaveChanges();
                return "删除分类成功";
            }
        }

        /// <summary>
        /// 修改分类
        /// </summary>
        /// <param name="GoodClass"></param>
        /// <param name="ChangeGoodClass"></param>
        /// <returns></returns>
        [HttpGet]
        public string ChangeClassifi(string GoodClass,string ChangeGoodClass)
        {
            var classifi=_db.Classifications.FirstOrDefault(x => x.GoodClass == GoodClass);
            if (classifi == null)
            {
                return "不存在该分类";
            }
            else
            {
                classifi.GoodClass = ChangeGoodClass;
                classifi.Classifi = ChangeGoodClass;
                _db.SaveChanges();
                return "修改成功";
            }
        }

        /// <summary>
        /// 查询分类
        /// </summary>
        /// <param name="classifid"></param>
        /// <returns></returns>
        [HttpGet]
        public List<Classification> SelectClassifi(string classifid)
        {
            if (classifid==null) {
                return _db.Classifications.ToList();
            }
            else
            {
                return _db.Classifications.Where(x=>x.GoodClass == classifid).ToList();
            }

        }

        /// <summary>
        /// 前端商品分类列表展示（列出商品分类数据）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<string> FountClassifi()
        {
            return _db.Classifications.Select(x=>x.GoodClass).ToList();
        }

        /// <summary>
        /// 前端商品列表展示（按照商品分类、商品名称搜索展示商品列表，可分页展示）
        /// </summary>
        /// <param name="classifi"></param>
        /// <param name="GoodName"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpGet]
        public List<Good> FoundSelectGoods(string classifi,string GoodName,int page)
        {
            if (classifi == null && GoodName != null)
            {
                var goodsCount = _db.Goods.Where(x => x.GoodName.Contains(GoodName)).Count();
                var skip = 0;
                var take = 0;
                if (page < 1)
                {
                    page = 1;
                }
                var endPage = 0;
                if (goodsCount % 6 != 0)
                {
                    endPage = goodsCount / 6 + 1;
                }
                else
                {
                    endPage = goodsCount / 6;
                }
                if (endPage == 1)
                {
                    skip = 1;
                    take = goodsCount;
                }
                if (page > endPage)
                {
                    page = endPage;
                    skip = page * 6 - 5;
                    take = goodsCount;
                }
                else
                {
                    skip = page * 6 - 6;
                    take = page * 6;
                }
                return _db.Goods.Where(x => x.GoodName.Contains(GoodName)).Skip(skip).Take(take).ToList();
            }
            else if (classifi != null && GoodName == null)
            {
                var goodsCount = _db.Goods.Where(x => x.GoodClass.Contains(classifi)).Count();
                var skip = 0;
                var take = 0;
                if (page < 1)
                {
                    page = 1;
                }
                var endPage = 0;
                if (goodsCount % 6 != 0)
                {
                    endPage = goodsCount / 6 + 1;
                }
                else
                {
                    endPage = goodsCount / 6;
                }
                if (endPage == 1)
                {
                    skip = 1;
                    take = goodsCount;
                }
                if (page > endPage)
                {
                    page = endPage;
                    skip = page * 6 - 5;
                    take = goodsCount;
                }
                else
                {
                    skip = page * 6 - 6;
                    take = page * 6;
                }
                return _db.Goods.Where(x => x.GoodClass.Contains(classifi)).Skip(skip).Take(take).ToList();
            }else if(classifi!=null&&GoodName != null)
            {
                var goodsCount = _db.Goods.Where(x => x.GoodClass.Contains(classifi)&&x.GoodName.Contains(GoodName)).Count();
                var skip = 0;
                var take = 0;
                if (page < 1)
                {
                    page = 1;
                }
                var endPage = 0;
                if (goodsCount % 6 != 0)
                {
                    endPage = goodsCount / 6 + 1;
                }
                else
                {
                    endPage = goodsCount / 6;
                }
                if (endPage == 1)
                {
                    skip = 1;
                    take = goodsCount;
                }
                if (page > endPage)
                {
                    page = endPage;
                    skip = page * 6 - 5;
                    take = goodsCount;
                }
                else
                {
                    skip = page * 6 - 6;
                    take = page * 6;
                }
                return _db.Goods.Where(x => x.GoodClass.Contains(classifi) && x.GoodName.Contains(GoodName)).Skip(skip).Take(take).ToList();
            }
            else
            {
                var goodsCount = _db.Goods.Count();
                var skip = 0;
                var take = 0;
                if (page < 1)
                {
                    page = 1;
                }
                var endPage = 0;
                if (goodsCount % 6 != 0)
                {
                    endPage = goodsCount / 6 + 1;
                }
                else
                {
                    endPage = goodsCount / 6;
                }
                if (endPage == 1)
                {
                    skip = 1;
                    take = goodsCount;
                }
                if (page > endPage)
                {
                    page = endPage;
                    skip = page * 6 - 5;
                    take = goodsCount;
                }
                else
                {
                    skip = page * 6 - 6;
                    take = page * 6;
                }
                return _db.Goods.Skip(skip).Take(take).ToList();
            }
        }


        /// <summary>
        /// 前端商品详情展示（用户点击商品进入商品详情，显示商品标题，图片，价格，库存，描述）
        /// </summary>
        /// <param name="goodid"></param>
        /// <returns></returns>
        [HttpGet]
        public string  FoundGoodsInfo(int goodid)
        {
            var good=_db.Goods.FirstOrDefault(x=>x.GoodId == goodid);
            if(good == null)
            {
                return "该商品不存在";
            }
            else
            {
                var goodname = good.GoodName;
                var goodimg = good.GoodImg;
                var price = good.GoodPrice;
                var inv = good.GoodInventory;
                var brief=good.GoodBrief;
                return "商品标题：" + good.GoodName + "\n商品图片：" + good.GoodImg + "\n商品价格：" + good.GoodPrice + "\n商品库存" + good.GoodInventory + "\n商品描述" + good.GoodBrief;
            }
        }
    }
}
